<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>流程部署</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">流程姓名</label>
                            <div class="layui-input-inline">
                                <input type="text" name="name" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary" lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-sm layui-btn-normal" lay-event="upload">上传流程文件</button>
                <button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="draw">在线绘制流程</button>
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="start">启动实例</a>
            <a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="show">查看</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="delete">删除部署</a>
        </script>
    </div>
</div>

<!-- 流程部署编辑弹窗 -->
<script type="text/html" id="processDepEditDialog">
    <form class="layui-form layuimini-form" enctype="multipart/form-data">
        <div class="layui-form-item">
            <label class="layui-form-label required">上传文件：</label>
            <div class="layui-input-block">
                <input id="fileName" name="fileName" class="layui-input dialog-input" disabled />
                <label type="button" class="layui-btn dialog-btn" for="uploadFile"><i class="layui-icon"></i>上传BPMN</label>
                <input id="uploadFile" type="file" name="processFile" class="layui-hide"/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">流程名称：</label>
            <div class="layui-input-block">
                <input name="deploymentName" placeholder="请输入流程名称" class="layui-input" lay-verify="required"/>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button type="button" class="layui-btn" lay-submit lay-filter="processDeptSubmit">提交</button>
                <button type="button" class="layui-btn layui-btn-primary" onclick="layer.closeAll();">返回</button>
            </div>
        </div>
    </form>
</script>

<!-- 启动流程实例弹窗 -->
<script type="text/html" id="startProcessInstDialog">
    <form class="layui-form layuimini-form">
        <div class="layui-form-item">
            <label class="layui-form-label">实例名称：</label>
            <div class="layui-input-block">
                <input name="instanceName" placeholder="请输入实例名称" class="layui-input" lay-verify="required"/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">实例描述：</label>
            <div class="layui-input-block">
                <input name="instanceVariable" placeholder="请输入实例描述" class="layui-input" lay-verify="required"/>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button type="button" class="layui-btn" lay-submit lay-filter="startProcessInstSubmit">提交</button>
                <button type="button" class="layui-btn layui-btn-primary" onclick="layer.closeAll()">返回</button>
            </div>
        </div>
    </form>
</script>
<script src="../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="../js/lay-config.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'table'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table;

        var insTb = table.render({
            elem: '#currentTableId',
            url: publicUrl + 'processDefinition/getDefinitions',
            toolbar: '#toolbarDemo',
            defaultToolbar: [],
            cols: [[
                {type: "checkbox", width: '5%', align: "center"},
                {field: 'id', width: '15%', title: '流程定义id', sort: true, align: "center"},
                {field: 'deploymentId', width: '15%', title: '流程部署id', sort: true, align: "center"},
                {field: 'name', width: '10%', title: '流程定义名称', align: "center"},
                {field: 'resourceName', width: '15%', title: '流程文件名称', align: "center"},
                {field: 'key', width: '14%', title: 'key', align: "center"},
                {field: 'version', width: '6%', title: '部署版本', align: "center"},
                {title: '操作', width: '20%', toolbar: '#currentTableBar', align: "center"}
            ]],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 10,
            page: true,
            skin: 'line',
            parseData: function (res) {
                return {
                    "code": 0,
                    "msg": res.msg,
                    "count": res.data.count,
                    "data": res.data.items
                };
            },
            done: function (res, curr, count) {
                if (res.code === 500) {
                    layer.alert(res.msg, {icon: 2, title: "提示"});
                }
            }
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            var result = JSON.stringify(data.field);

            //执行搜索重载
            table.reload('currentTableId', {
                page: {
                    curr: 1
                },
                where: {
                    params: result
                }
            }, 'data');

            return false;
        });

        // toolbar监听事件
        table.on('toolbar(currentTableFilter)', function (obj) {
            // 上传流程文件
            if (obj.event === 'upload') {
                uploadProcessDialog();
            } else if (obj.event === 'draw') {  // 在线绘制流程
                window.open(publicUrl + 'bpmnjs/dist/index.html?type=addBPMN');
            }
        });

        // 监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj)
        });

        // tool监听事件
        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;

            // 启动实例
            if (obj.event === 'start') {
                startProcessInstDialog(obj);
            } else if (obj.event === 'show') { // 查看流程
                window.open(publicUrl + 'bpmnjs/dist/index.html?type=showBPMN&deploymentId=' + data.deploymentId + '&resourceName=' + encodeURI(data.resourceName));
            } else if (obj.event === 'delete') { // 删除部署
                deleteDeployment(obj);
            }
        });

        /**
         * 上传流程文件
         */
        function uploadProcessDialog() {
            layer.open({
                title: '上传流程文件',
                type: 1,
                shade: 0.2,
                maxmin:true,
                shadeClose: true,
                content: $('#processDepEditDialog').html(),
                success: function (layero, index) {
                    var $fileName = $('#fileName');
                    $fileName.val('');
                    $('#uploadFile').on('change', function (e) {
                        var e = e || window.event;
                        var files = e.target.files;
                        if (files.length > 0) {
                            // 获取文件名并显示
                            var fileName = files[0].name;
                            $fileName.val(fileName);
                        } else {
                            // 清空文件名
                            $fileName.val('');
                        }
                    });
                    // 提交表单
                    form.on('submit(processDeptSubmit)', function (data) {
                        var fileObj = document.getElementById('uploadFile').files[0];
                        if (typeof fileObj == 'undefined' || fileObj.size <= 0) {
                            layer.msg('请选择文件');
                            return;
                        }
                        var formData = new FormData();
                        formData.append("processFile", fileObj);
                        formData.append("deploymentName", data.field.deploymentName);

                        var loadIndex = layer.load(2);
                        $.ajax({
                            cache: true,
                            type: "post",
                            url: publicUrl + 'processDefinition/uploadStreamAndDeployment',
                            async: false,
                            data: formData,
                            contentType: false,
                            processData: false,
                            success: function (res) {
                                layer.close(loadIndex);
                                if (res.code === 200) {
                                    layer.close(index);
                                    layer.msg(res.msg, {icon: 1});
                                    insTb.reload();
                                } else {
                                    layer.alert(res.msg, {icon: 2, title: '提示'});
                                }
                            },
                            error: function (res) {
                                layer.close(loadIndex);
                                layer.alert(res.msg, {icon: 2, title: '提示'});
                            }
                        });
                        return false;
                    });
                    // 禁止弹窗出现滚动条
                    $(layero).children('.layui-layer-content').css('overflow', 'visible');
                }
            });
        }

        /**
         * 启动实例
         */
        function startProcessInstDialog(obj) {
            layer.open({
                title: '启动流程部署信息',
                type: 1,
                shade: 0.2,
                maxmin:true,
                shadeClose: true,
                content: $('#startProcessInstDialog').html(),
                success: function (layero, index) {
                    // 提交表单
                    form.on('submit(startProcessInstSubmit)', function (data) {
                        var loadIndex = layer.load(2);
                        $.post(publicUrl + 'processInstance/startInstance', {
                            processDefinitionKey: obj.data.key,
                            instanceName: data.field.instanceName,
                            instanceVariable: data.field.instanceVariable
                        }, function (res) {
                            layer.close(loadIndex);
                            if (res.code === 200) {
                                layer.close(index);
                                layer.msg(res.msg, {icon: 1});
                                insTb.reload();
                            } else {
                                layer.alert(res.msg, {icon: 2, title: '提示'});
                            }
                        }, 'json');
                        return false;
                    });
                    // 禁止弹窗出现滚动条
                    $(layero).children('.layui-layer-content').css('overflow', 'visible');
                }
            });
        }

        /**
         * 删除流程部署
         * @param obj
         */
        function deleteDeployment(obj) {
            layer.confirm('确定要删除选中数据吗？', {
                skin: 'layui-layer-admin',
                shade: .1
            }, function (index) {
                layer.close(index);
                var loadIndex = layer.load(2);
                $.post(publicUrl + 'processDefinition/deleteDeployment', {
                    deploymentId: obj.data ? obj.data.deploymentId : ''
                }, function (res) {
                    layer.close(loadIndex);
                    if (res.code === 200) {
                        layer.msg(res.msg, {icon: 1});
                        obj.del();
                        insTb.reload();
                    } else {
                        layer.alert(res.msg, {icon: 2, title: '提示'});
                    }
                }, 'json');
            });
        }
    });
</script>

</body>
</html>